package com.fengmi.controller;

import com.fengmi.dto.PageDTO;
import com.fengmi.entity.SysRole;
import com.fengmi.service.SysRoleService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 123
 * @Date 2022/1/25 10:04
 * @Version 1.0
 */

@RestController
@RequestMapping("role")
@CrossOrigin
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @PostMapping("page")
    @ApiOperation("分页及条件查询")
    public PageResultVO<SysRole> page(@RequestBody PageDTO<SysRole> pageDTO) {
        return roleService.findRoleByPage(pageDTO);
    }

    @GetMapping("enable/{roleCode}/{enable}")
    @ApiOperation("修改角色的可用状态")
    public ResultVO toggleEnable(@PathVariable String roleCode, @PathVariable Integer enable) {
        return roleService.updateRoleEnable(roleCode, enable);
    }

    @PostMapping("add")
    @ApiOperation("添加角色信息")
    public ResultVO saveAddRole(@RequestBody SysRole sysRole) {
        return roleService.saveAddRole(sysRole);
    }

    @PutMapping("updateInfo")
    @ApiOperation("修改角色基本信息")
    public ResultVO updateBasicInfo(@RequestBody SysRole sysRole) {
        return roleService.saveUpdateRoleBasicInfo(sysRole);
    }

    @GetMapping("pers/{roleId}")
    @ApiOperation("通过角色id查询所有权限")
    public ResultVO findAllPermissionByRoleId(@PathVariable String roleId) {
        return roleService.selectPermissionsByRoleId(roleId);
    }

    @PostMapping("allotPerm/{roleCode}")
    @ApiOperation("角色分配权限")
    public ResultVO allotPermissions(@PathVariable String roleCode, @RequestBody String[] permissions) {
        return roleService.saveRolesPermissions(roleCode, permissions);
    }

    @GetMapping("all")
    @ApiOperation("查询所有角色信息")
    public ResultVO findAllRole() {
        return roleService.selectAllRoles();
    }

}

